using System;
using System.Collections.Generic;
using System.Collections;
using System.Xml.Serialization;
using System.Text;
using System.IO;

namespace Level65.ClipShow
{


    [Serializable]
    public class ThumbnailWork
    {
        [XmlElement(Type = typeof(ThumbnailPayloadItem))]
        public ArrayList ThumbNailPayload = new ArrayList();
    }

    [Serializable]
    public class ThumbnailPayloadItem
    {

        public ThumbnailPayloadItem()
        {
        }

        [XmlElement]
        public string ThumbnailTargetFilePath;

        [XmlElement]
        public string SourceMediaFilePath;

        [XmlElement]
        public Int32 StartFrame = 0;

        [XmlElement]
        public string Title = "";
    }

    [Serializable]
    public class Settings
    {
        private MediaClipLengths _defaultLength = MediaClipLengths.All;     // Changed to all for default
        private bool _useEffects = false;       // Turn off effects on default
        private bool _useVMCFullScreen = true;
        private bool _createAsx = false;
        private bool _indexRecordedTv = false;
        private string[] _mediaTypes = { "*.wmv", "*.avi", "*.mov", "*.mp*", "*.mts", "*.m2ts", ".m2t" };
        private string[] _recordedTvTypes = { "*.dvr-ms", "*.wtv" };

        public Settings()
        {
        }

        [XmlElement]
        public string[] MediaTypes
        {
            get { return (_mediaTypes); }
            set { _mediaTypes = value; }
        }

        [XmlElement]
        public string[] RecordedTvTypes
        {
            get { return (_recordedTvTypes); }
            set { _recordedTvTypes = value; }
        }

        [XmlIgnore]
        public string[] AllMediaTypes
        {
            get
            {
                string[] results = new string[_mediaTypes.Length + _recordedTvTypes.Length];
                int Ctr = 0;
                foreach (string s in _mediaTypes)
                {
                    results[Ctr] = s;
                    Ctr++;
                }
                foreach (string s in _recordedTvTypes)
                {
                    results[Ctr] = s;
                    Ctr++;
                }

                return (results);
            }
            set { _recordedTvTypes = value; }
        }

        [XmlAttribute]
        public bool IndexRecordedTv
        {
            get { return (_indexRecordedTv); }
            set { _indexRecordedTv = value; }
        }

        [XmlAttribute]
        public bool CreateAsx 
        {
            get { return (_createAsx); }
            set { _createAsx = value; }
        }
        
        [XmlAttribute]
        public bool UseVMCFullScreen
        {
            get
            {
                return (_useVMCFullScreen);
            }
            set
            {
                _useVMCFullScreen = value;
            }
        }

        [XmlAttribute]
        public MediaClipLengths DefaultClipLength
        {
            get
            {
                return (_defaultLength);
            }
            set
            {
                _defaultLength = value;
            }
        }

        [XmlAttribute]
        public bool UseEffects
        {
            get { return (_useEffects); }
            set { _useEffects = value; }
        }

        [XmlIgnore]
        public string NextClipLength
        {
            get
            {
                string result = "";
                if (_defaultLength.Equals(MediaClipLengths.Short))
                {
                    result = "Medium (45 seconds)";
                }
                if (_defaultLength.Equals(MediaClipLengths.Medium))
                {
                    result = "Long (1 minute)";
                }
                if (_defaultLength.Equals(MediaClipLengths.Long))
                {
                    result = "All (entire length of media item)";
                }
                if (_defaultLength.Equals(MediaClipLengths.All))
                {
                    result = "Short (30 seconds)";
                }
                return (result);
            }
        }
    }

    [Serializable]
    public class ClipItem
    {
        private string _keywords = "";
        private long _videoCount = 0;
        private string _topicName = "";
        private string _thumbnail = "";
        private string _mediaChannelSource = "";

        private string ItemNumber(long Count)
        {
            if (Count > 1)
            {
                return ("items");
            }
            else
            {
                return ("item");
            }
        }

        public string MediaChannelUri
        {
            get
            {
                return (_mediaChannelSource);
            }
            set
            {
                _mediaChannelSource = value;
            }
        }
        public string LongName
        {
            get
            {
                return (String.Format("{0} ({1} {2})",
                    this.Name, this.Count, ItemNumber(this.Count)));
            }
        }

        [XmlAttribute]
        public string ThumbnailUrl
        {
            get
            {
                return (_thumbnail);
            }
            set
            {
                _thumbnail = value;
            }
        }
        [XmlAttribute]
        public long Count
        {
            get { return (_videoCount); }
            set { _videoCount = value; }
        }

        [XmlAttribute]
        public string Name
        {
            get { return (_topicName); }
            set { _topicName = value; }
        }

        [XmlAttribute]
        public string Keywords
        {
            get { return (_keywords); }
            set { _keywords = value; }
        }

        [XmlElement(Type = typeof(MediaMetaData))]
        public ArrayList MediaItems = new ArrayList();
    }

    [Serializable]
    public enum MediaClipLengths { Short = 30000, Medium = 60000, Long = 90000, All = 0 }

    [Serializable]
    public class ClipCollection
    {
        private string _channelName;
        private MediaClipLengths _clipLength = MediaClipLengths.Medium;
        private string _mediaSourceFilePath = "";

        public ClipCollection()
        {

        }

        [XmlElement(Type = typeof(ClipItem))]
        public ArrayList Items = new ArrayList();


        [XmlAttribute]
        public string MediaSourceFilePath
        {
            get { return (_mediaSourceFilePath); }
            set { _mediaSourceFilePath = value; }
        }

        [XmlAttribute]
        public string Name
        {
            get { return (_channelName); }
            set { _channelName = value; }
        }

        [XmlIgnore]
        public bool PlayClips
        {
            get
            {
                return (!_clipLength.Equals(MediaClipLengths.All));
            }
        }
        [XmlIgnore]
        public MediaClipLengths ClipLength
        {
            get { return (_clipLength); }
            set { _clipLength = value; }
        }

        public void IncrementClipLength()
        {
            if (_clipLength.Equals(MediaClipLengths.Long))
            {
                _clipLength = MediaClipLengths.All;
                return;
            }
            if (_clipLength.Equals(MediaClipLengths.Medium))
            {
                _clipLength = MediaClipLengths.Long;
                return;
            }
            if (_clipLength.Equals(MediaClipLengths.Short))
            {
                _clipLength = MediaClipLengths.Medium;
                return;
            }
            else
            {
                _clipLength = MediaClipLengths.Short;
            }
        }

        public int ClipLengthInSeconds
        {
            get
            {
                if (_clipLength.Equals(MediaClipLengths.Long))
                {
                    return (90000);
                }
                if (_clipLength.Equals(MediaClipLengths.Medium))
                {
                    return (60000);
                }
                if (_clipLength.Equals(MediaClipLengths.Short))
                {
                    return (30000);
                }
                else
                {
                    return (0);
                }
            }
        }

        public void CreateASX(string destinationFilePath)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("<asx version=\"1.0\">");
            sb.AppendFormat("<title>{0}</title>", _channelName);
            ClipItem source = (ClipItem)Items[0];
            string thumbnailUrl = "";
            foreach (MediaMetaData med in source.MediaItems)
            {
                if (thumbnailUrl.Equals(""))
                {
                    if (File.Exists(med.ThumbnailUrl))
                    {
                        thumbnailUrl = med.ThumbnailUrl;
                    }
                }
                sb.AppendFormat("<entry><title>{0}</title><ref href=\"{1}\"/><author>{2}</author></entry>",
                    med.Title, med.Url, med.Author);
            }
            sb.AppendFormat("</asx>");

            try
            {
                string filePath = String.Format("{0}\\{1}.asx", destinationFilePath, _channelName);
                TextWriter writer = new StreamWriter(filePath);
                writer.Write(sb.ToString());
                writer.Close();

                string asxThumbnail = String.Format("{0}\\{1}.jpg", destinationFilePath, _channelName);
                File.Copy(thumbnailUrl, asxThumbnail);
            }
            catch (SystemException se)
            {
                System.Diagnostics.EventLog.WriteEntry("clipShow.CreateASX", se.Message);
            }
        }

        
    }
}